/* TurnoutCompetition.do */

* Global options.
global OUTREG2OPTIONS tex(fragment) sdec(1) 2aster auto(1) rdec(3) label
global TABLELOC "."
global FE_options fe cluster(Party_State)

use "TurnoutCompetition.dta", clear

*
* tsset Diff in diff by party-state-year.
*
tsset Party_State cycle, delta(2)

*****************************
* Regression models
*****************************

* Loop over different specifications.
forvalues modelgroups = 1(1)3 {
  * Determine specification.
	if `modelgroups'==1 {
    * My coding of less_costly and nonpartisan.
    local EVs = "less_costly_primary nonpartisan_primary"
    local outregfile "$TABLELOC/Table03"
    local outregfile2 "$TABLELOC/Table06"
    local note "Excluded category is institutions most costly for individual participation."
  }
  else if `modelgroups'==2 {
    * Original McGheet et al. coding. Closed excluded.
    local EVs = "ptype_*"
    local outregfile "$TABLELOC/TableA05"
    local outregfile2 "$TABLELOC/TableA08"
    local note "Excluded category is closed primary."
  } 
  else if `modelgroups'==3 {
    * Dynamic lag models.
    local EVs = "less_costly_primary nonpartisan_primary lag* lead*"
    local outregfile "$TABLELOC/TableA02"
    local outregfile2 "$TABLELOC/TableA04"
    local note "Excluded category is institutions most costly for individual participation."
  }

  local keepvars "`EVs'"
  
  ************************
  * First table: turnout.
  ************************
  
  * Turnout (total) of VEP.
  xtreg state_turnout_vep `EVs' `CVs' i.cycle, $FE_options
   outreg2 using `outregfile'.tex, $OUTREG2OPTIONS ctitle("Total primary,Votes cast as,Percent of,Voting Eligible") addnote("OLS coefficients with robust standard errors clustered on state-party in parentheses.","`note'") nocons keep(`keepvars') addtext("State FEs","Yes","Election cycle FEs","Yes") replace

  * Turnout (partisan) of VEP.
  xtreg turnout_vep `EVs' `CVs' i.cycle, $FE_options
   outreg2 using `outregfile'.tex, $OUTREG2OPTIONS ctitle("Major party,Votes cast as,Percent of,Voting Eligible") nocons keep(`keepvars') addtext("Party-state FEs","Yes","Election cycle FEs","Yes") append
   
  * By party.
  xtreg turnout_vep `EVs' `CVs' i.cycle if party == "Dem", $FE_options
   outreg2 using `outregfile'.tex, $OUTREG2OPTIONS ctitle("Democratic,Votes cast as,Percent of,Voting Eligible") nocons keep(`keepvars') addtext("Party-state FEs","Yes","Election cycle FEs","Yes") append

  xtreg turnout_vep `EVs' `CVs' i.cycle if party == "Rep", $FE_options
   outreg2 using `outregfile'.tex, $OUTREG2OPTIONS ctitle("Republican,Votes cast as,Percent of,Voting Eligible") nocons keep(`keepvars') addtext("Party-state FEs","Yes","Election cycle FEs","Yes") append
 
  ************************
  * Second table: measures of primary contestation/competition.
  ************************
  * Rate contested.
  cap qui gen pct_contested = 100*rate_contested
  xtreg pct_contested `EVs' `CVs' i.cycle, $FE_options
   outreg2 using `outregfile2'.tex, $OUTREG2OPTIONS ctitle("Percentage,House,Primaries,Contested") addnote("OLS coefficients with robust 95% confidence interval clustered on state-party.","Contested primary defined as more than one non-write-in candidate.","`note'") stats(coef ci) bracket nocons keep(`keepvars') addtext("Party-state FEs","Yes","Election cycle FEs","Yes") replace
  
  * Number of candidates.
  xtreg num_cands `EVs' `CVs' i.cycle, $FE_options
   outreg2 using `outregfile2'.tex, $OUTREG2OPTIONS ctitle("Number,House,Primary,Candidates") stats(coef ci) bracket nocons keep(`keepvars') addtext("Party-state FEs","Yes","Election cycle FEs","Yes") append
  * Log version.
  qui capture g log_num_cands = log(num_cands)
  xtreg log_num_cands `EVs' `CVs' i.cycle, $FE_options
   outreg2 using `outregfile2'.tex, $OUTREG2OPTIONS ctitle("Log,Primary,Candidates") stats(coef ci) bracket nocons keep(`keepvars') addtext("Party-state FEs","Yes","Election cycle FEs","Yes") append
  
  * Average winning margin (percent).
  xtreg avg_win_margin_pct `EVs' `CVs' i.cycle, $FE_options
   outreg2 using `outregfile2'.tex, $OUTREG2OPTIONS ctitle("Average,Winning,Margin,(Percent)") stats(coef ci) bracket nocons keep(`keepvars') addtext("Party-state FEs","Yes","Election cycle FEs","Yes") append
}

* Summary statistics.
summ state_turnout_vep, detail
summ rate_contested, detail


